package com.biao.mall.order.controller;

import com.biao.mall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.aspectj.weaver.ast.Or;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Controller
public class TestController {
    @Autowired
    RabbitTemplate rabbitTemplate;


    /**
     * 发送订单到order-event-exchange队列
     * 一分钟后成死信
     * 发送到order.release.order.queue队列
     * 监听获取
     */
    @ResponseBody
    @GetMapping("order/test")
    public String createOrder(){
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(UUID.randomUUID().toString());
        orderEntity.setPaymentTime(new Date());
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderEntity);
        return "ok ";
    }

//    @RabbitListener(queues = "order.release.order.queue")
//    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
//        System.out.println("收到的过期订单消息:准备关闭订单"+entity.getOrderSn());
//        System.out.println(entity);
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }


    @ResponseBody
    @GetMapping("/sendMes")
    public String sendMessage() {
        rabbitTemplate.convertAndSend("java.hello.exchange.direct", "mq.news", "123");
//        rabbitTemplate.convertAndSend("java.hello.exchange.direct", "mq.news1", "123");
        return "ok";
    }


    @GetMapping("/page/{page}")
    public String page(@PathVariable("page") String page) {
        return page;
    }
}
